package com.disease.chronic.Service.Impl;

import com.disease.chronic.Exception.ChronicExceptionEnum;
import com.disease.chronic.Global.Result;
import com.disease.chronic.Mappers.IndicatorsMapper;
import com.disease.chronic.Mappers.UserMapper;
import com.disease.chronic.Service.UserService;
import com.disease.chronic.pojo.Indicators;
import com.disease.chronic.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    IndicatorsMapper indicatorsMapper;



    @Autowired
    UserMapper userMapper;
    @Override
    public Result register(User user) {
        User user1 = userMapper.selectByPrimaryKey(user.getUsername());
        if(user1!=null){
            return new Result(ChronicExceptionEnum.USER_EXIST.getCode(),
                    ChronicExceptionEnum.USER_EXIST.getMessage());
        }
        userMapper.insert(user);
        return new Result(ChronicExceptionEnum.SUCCESS.getCode(),
                ChronicExceptionEnum.SUCCESS.getMessage());
    }

    @Override
    public Result login(User user) {
        User user1 = userMapper.selectByPrimaryKey(user.getUsername());
        if(user1 == null||!user1.getPassword().equals(user.getPassword())){
            return new Result(ChronicExceptionEnum.PASSWORD_WRONG.getCode(),
                    ChronicExceptionEnum.PASSWORD_WRONG.getMessage());
        }
        return new Result(ChronicExceptionEnum.SUCCESS.getCode(),
                ChronicExceptionEnum.SUCCESS.getMessage());
    }

    @Override
    public Result getState(String username) {
        Date today = new Date(System.currentTimeMillis());
        List<Indicators> indicators = indicatorsMapper.selectByUsernameAndDate(username,today);
        int state = 1;
        if(indicators.isEmpty()){
            state = 3;
        } else if (indicators.size()<5) {
            state = 2;
        }
        return new Result(ChronicExceptionEnum.SUCCESS.getCode(),
                ChronicExceptionEnum.SUCCESS.getMessage(),state);

    }

    @Override
    public User getUser(String username) {
        return userMapper.selectByPrimaryKey(username);
    }
}
